Ext.onReady(function(){
	
	Ext.BLANK_IMAGE_URL = 'resources/images/default/s.gif';
	var cslcode = '';
	
	var CStore = new Ext.data.JsonStore({
		url:'combox/getstore?flag=yes',
		fields: ["classcode","classname"]
	});
	CStore.load();
	
	var ctreepanelloader = new Ext.tree.TreeLoader({
										url: 'tree/getByClasscode?clscode=emptyclass',
										requestMethod: 'GET'
	});
	
	var cob = new Ext.form.ComboBox({
									name:'clssssid',
									id:'clssssid',
									typeAhead:true,
									mode:'local',
									triggerAction:'all',
									editable:false,
									store:CStore,
									displayField:'classname',
									emptyText:'请先选择合适的分类',
									selectOnFocus:true,
									width:135
	});
	
	var trpa = new Ext.tree.TreePanel({
										        		el:'tree-div',
										        		title:'分类结构',
										        		height:400,
										        		width:500,
										            	rootVisible: false,
										            	lines: true,
										        		autoScroll:true,  
										        		animate:true,  
										        		enableDD:false,
										        		containerScroll: true,  
										        		border : true, 
										        		loader: ctreepanelloader,
														tbar:['请选择分类：',cob]
	});
	
	var root = new Ext.tree.AsyncTreeNode({
		id:'0',
		text:'编码类别',
		classcode:'',
		expanded:true
	});

	trpa.setRootNode(root);
	trpa.render();
	trpa.expand(false,false);
	
	trpa.on('beforeload',function(_node){
		ctreepanelloader.url = 'tree/getnode?pid=' + _node.id +'&classcode=' + cslcode;
	});
	
	cob.on('select',function(co,rec,nid){
		cslcode=rec.get("classcode");
		root.reload();
	});
	
	var frm = new Ext.form.FormPanel({
													applyTo:'upwin',
													fileUpload: true,
											        bodyStyle: 'padding: 10px 10px 0 10px',
											        labelWidth: 70,
											        autoHeight:true,
											        width:500,
											        defaults: {
											            allowBlank: false,
											           	anchor:'95%',
											           	msgTarget:'side'
											        },
											        items: [{
												        	xtype: 'hidden',
												        	name: 'code'
												        },{
												            xtype: 'textfield',
												            name : 'text',
												            readOnly:true,
												            fieldLabel: '类别名称'
												        },{
												            xtype: 'fileuploadfield',
												            emptyText: 'Select an image',
												            fieldLabel: '示 意 图',
												            name: 'img-path',
												            buttonCfg: {
												                text: '',
												                iconCls: 'upload-icon'
												            }
												        }
												        ],
												     buttons: [{
										                    text     : '提交',
										                    handler  : function(){
										                    			frm.getForm().submit({
																			url:'cls/img',
																			method:'POST',
																			success:function(){
																				Ext.msg.alert("info","上传图片成功！");
																			},
																			failure:function(){
																				Ext.msg.alert("info","上传图片失败！");
																			},
																			waitMsg:'正在提交数据，请稍候。。。'
																		});
//										                    	alert(frm.findByType("hidden")[0].value);
										                    }
										                }]
	});
	
//	var node2Record = new Ext.data.Record.create(
//														    {name: 'text', mapping: 'text'},
//														    {name: 'code', mapping:'code'}
//												);
	
	trpa.getSelectionModel().on('selectionchange',function(sm,node){
//		var r = new node2Recordmodel({
//									code:node.attributes["code"],
//									text:node.text
//		});
		
		frm.findByType("hidden")[0].setValue(node.attributes["code"]);
		
		frm.findByType("textfield")[0].setValue(node.text);
	});
});